//
//  HWGameLiveChatGameHeader.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/1/17.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import Cosmos
class HWGameLiveChatGameHeader: UITableViewHeaderFooterView {

    var bgImageView:UIImageView!
    var gameLogoImageView:UIImageView!
    var gameNameLabel:UILabel!
    var starImageView:UIImageView!
    var scoreLabel:UILabel!
    var typeLabel:UILabel!
    var recommendLabel:UILabel!
    var starView:CosmosView!
    var downLoadButton:UIButton!
    var gameModel:HWVoiceRoomGameModel?{
        didSet {
            gameLogoImageView.sd_setImage(with: URL(string: gameModel?.icon ?? ""), completed: nil)
            gameNameLabel.text = gameModel?.name
            scoreLabel.text = gameModel?.point
            starView.rating = gameModel?.tuijianzhishu ?? 0
            typeLabel.text = gameModel?.type
        }
    }
    
    var downLoadButtonClick:(()->())?
    
    class func headerOrFooterWithTableView(tableView:UITableView) -> (HWGameLiveChatGameHeader) {
        let reUse:String = "HWGameLiveChatGameHeader"
        
        var headerOrFooter = tableView.dequeueReusableHeaderFooterView(withIdentifier: reUse)
        
        if headerOrFooter == nil {
            headerOrFooter = HWGameLiveChatGameHeader.init(reuseIdentifier: reUse)
        }
        
        return headerOrFooter as! HWGameLiveChatGameHeader
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        initUI()
        initFrame()
    }
    
    
    func initUI() -> Void {
        
        contentView.backgroundColor = UIColor.colorWithHexString(hexString: "#FFFEFF")
        
        bgImageView = UIImageView()
        bgImageView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 72*widthScale)
        bgImageView.image = UIImage(named: "chatGameBg")
        bgImageView.isUserInteractionEnabled = true
        contentView.addSubview(bgImageView)
        
//        UIColor.gradientColor(view: bgImageView, colors: [UIColor.colorWithHexString(hexString: "#FEFAF0").cgColor,UIColor.colorWithHexString(hexString: "#FFFCF6").cgColor], start: CGPoint(x: 0, y: 0.16), end: CGPoint(x: 1, y: 1))
        
        gameLogoImageView = UIImageView()
        gameLogoImageView.image = UIImage(named: "")
        gameLogoImageView.layer.cornerRadius = 5
        bgImageView.addSubview(gameLogoImageView)
        
        gameNameLabel = HWLabel.createBoldLabel(title: "", color: "#444444", font: 14*widthScale)
        bgImageView.addSubview(gameNameLabel)
        
        starImageView = UIImageView()
        starImageView.image = UIImage(named: "homeStar")
        bgImageView.addSubview(starImageView)
        
        scoreLabel = HWLabel.createLabel(title: "", color: "#444444", font: 10*widthScale)
        bgImageView.addSubview(scoreLabel)
        
        typeLabel = HWLabel.createLabel(title: "", color: "#888888", font: 10*widthScale)
        bgImageView.addSubview(typeLabel)
        
        
        
        recommendLabel = HWLabel.createLabel(title: "推荐指数：", color: "#444444", font: 10*widthScale)
        bgImageView.addSubview(recommendLabel)
        
        starView = CosmosView.init()
        starView.rating = 0
        starView.settings.filledImage = UIImage(named: "fillStar")
        starView.settings.emptyImage = UIImage(named: "emptyStar")
        starView.settings.starSize = 11
        starView.settings.starMargin = 0
        starView.isUserInteractionEnabled = false
        starView.didFinishTouchingCosmos = {
            rating in

        }
        bgImageView.addSubview(starView)
        
        downLoadButton = UIButton(type: .custom)
        downLoadButton.backgroundColor = .white
        downLoadButton.setTitle("下载主播\n同款游戏", for: .normal)
        downLoadButton.titleLabel?.numberOfLines = 2
        downLoadButton.setTitleColor(.colorWithHexString(hexString: "#F87706"), for: .normal)
        downLoadButton.titleLabel?.font = .systemFont(ofSize: 12*widthScale)
        bgImageView.addSubview(downLoadButton)
        downLoadButton.layer.borderWidth = 1
        downLoadButton.layer.cornerRadius = 4*widthScale
        downLoadButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#F87706").cgColor
        downLoadButton.addTarget(self, action: #selector(downLoadButtonClick(_:)), for: .touchUpInside)
    }
    
    func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(0)
            make.bottom.equalTo(-20*widthScale)
        }
        
        gameLogoImageView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(50*widthScale)
        }
        
        gameNameLabel.snp.makeConstraints { make in
            make.top.equalTo(10*widthScale)
            make.left.equalTo(gameLogoImageView.snp.right).offset(12*widthScale)
        }
        
        starImageView.snp.makeConstraints { make in
            make.left.equalTo(gameNameLabel)
            make.top.equalTo(gameNameLabel.snp.bottom).offset(7*widthScale)
            make.width.height.equalTo(8*widthScale)
        }
        
        scoreLabel.snp.makeConstraints { make in
            make.left.equalTo(starImageView.snp.right).offset(2*widthScale)
            make.centerY.equalTo(starImageView)
        }
        
        typeLabel.snp.makeConstraints { make in
            make.centerY.equalTo(starImageView)
            make.left.equalTo(scoreLabel.snp.right).offset(8*widthScale)
        }
        
        recommendLabel.snp.makeConstraints { make in
            make.left.equalTo(gameNameLabel)
            make.top.equalTo(starImageView.snp.bottom).offset(7*widthScale)
        }
        
        starView.snp.makeConstraints { make in
            make.centerY.equalTo(recommendLabel)
            make.left.equalTo(recommendLabel.snp.right)
        }
        
        downLoadButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-19*widthScale)
            make.width.equalTo(78*widthScale)
            make.height.equalTo(38*widthScale)
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    
    @objc func downLoadButtonClick(_:UIButton) -> Void {
        self.downLoadButtonClick?()
    }
}
